package com.helloworld;

import com.rabbitmq.client.*;
import com.utils.RabbitMQConnectionUtil;
import org.junit.Test;

import java.io.IOException;

public class Consumer {

    @Test
    public void consume() throws Exception {
        Connection connection = RabbitMQConnectionUtil.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(Publisher.QUEUE_NAME, false, false, false, null);
        channel.basicConsume(Publisher.QUEUE_NAME, false, new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者获取到消息：" + new String(body,"UTF-8"));
            }
        });
        System.out.println("开始监听队列");
        System.in.read();

//        //1. 获取连接对象
//        Connection connection = RabbitMQConnectionUtil.getConnection();
//
//        //2. 构建Channel
//        Channel channel = connection.createChannel();
//
//        //3. 构建队列
//        channel.queueDeclare(Publisher.QUEUE_NAME,false,false,false,null);
//
//        //4. 监听消息
//        DefaultConsumer callback = new DefaultConsumer(channel){
//            @Override
//            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//                System.out.println("消费者获取到消息：" + new String(body,"UTF-8"));
//            }
//        };
//        channel.basicConsume(Publisher.QUEUE_NAME,true,callback);
//        System.out.println("开始监听队列");
//
//        System.in.read();
    }
}